package com.drug.mingkang.service.impl;

import com.drug.mingkang.mapper.*;
import com.drug.mingkang.model.*;
import com.drug.mingkang.model.vo.AuditRecordVo;
import com.drug.mingkang.model.vo.ImmPurchaseVo;
import com.drug.mingkang.model.vo.PurchaseorderVo;
import com.drug.mingkang.model.vo.PurchasereviewVo;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.AuditRecordService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 审核记录表(AuditRecord)表服务实现类
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:12
 */
@Service("auditRecordService")
public class AuditRecordServiceImpl implements AuditRecordService {
    @Resource
    Tools tools;
    @Resource
    private AuditRecordMapper auditRecordMapper;

    @Resource
    private HqPurchaseorderMapper hqPurchaseorderMapper;

    /**
     * 退货订单
     */
    @Resource
    private OrderReturnApplyMapper orderReturnApplyMapper;
    /**
     * 总公司销售订单
     */
    @Resource
    private HqMarketMapper hqMarketMapper;
    /**
     * 分公司订单
     */
    @Resource
    private ImmPurchaseMapper immPurchaseMapper;
    /**
     * 总公司财务
     */
    @Resource
    private HqFinancingMapper hqFinancingMapper;
    /**
     * 分公司财务
     */
    @Resource
    ImmFinanceMapper immFinanceMapper;
    /**
     * 总公司财务明细
     */
    @Resource
    HqRicherMapper hqRicherMapper;

    @Override
    public boolean updateState(String id, int isva, String reviewRemark) {
        return this.auditRecordMapper.updateState(id, isva, reviewRemark);
    }

    @Override
    public Map<String, Object> queryAllRawMaterialByLimit(PurchaseorderVo purchaseorderVo) {
        return tools.limit(this.hqPurchaseorderMapper.queryAuditAllByLimit(purchaseorderVo), this.hqPurchaseorderMapper.count(purchaseorderVo));
    }


    @Override
    public List<HqReview> rawMaterialShowAll(PurchasereviewVo purchasereviewVo) {
        return this.auditRecordMapper.rawMaterialShowAll(purchasereviewVo);
    }

    /**
     * 查询多条数据
     *
     * @param auditRecordVo 审核vo
     * @return 对象列表
     */
    @Override
    public Map<String, Object> queryAllByLimit(AuditRecordVo auditRecordVo) {
        return tools.limit(this.auditRecordMapper.queryAllByLimit(auditRecordVo), this.auditRecordMapper.count(auditRecordVo));
    }

    @Override
    public int selectState(String purchaseId) {
        return this.auditRecordMapper.selectState(purchaseId);
    }

    @Override
    public boolean deleteByPurchaseId(String purchaseId) {
        return this.auditRecordMapper.deleteByPurchaseId(purchaseId);
    }

    @Override
    public int selectrawMaterialCount(PurchasereviewVo purchasereviewVo) {
        return this.auditRecordMapper.selectrawMaterialCount(purchasereviewVo);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AuditRecord queryById(String id) {
        return this.auditRecordMapper.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param auditRecord 实例对象
     * @return 实例对象
     */
    @Override
    public AuditRecord insert(AuditRecord auditRecord) {
        this.auditRecordMapper.insert(auditRecord);
        return auditRecord;
    }

    @Override
    public boolean inserts(AuditRecord auditRecord) {
        return this.auditRecordMapper.inserts(auditRecord);
    }

    /**
     * 修改数据
     *
     * @param auditRecord 实例对象
     * @return 实例对象
     */
    @Override
    public AuditRecord update(AuditRecord auditRecord) {
        this.auditRecordMapper.update(auditRecord);
        return this.queryById(auditRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.auditRecordMapper.deleteById(id) > 0;
    }



    /**
     * 修改退货财务状态
     * @param immPurchaseVo Vo
     * @author Mr.wang
     * @return
     */
    @Override
    public ResponseMessage updateFinanceIsvaReturn(ImmPurchaseVo immPurchaseVo, AuditRecord auditRecord, HttpSession session) {
        System.out.println(immPurchaseVo.getPurchaseId());
        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
        String companyId = perUsers.getCompanyId();
        String userName = perUsers.getUserName();
        Integer zizi = Integer.valueOf(immPurchaseVo.getIsva());
        //财务审核通过
       if (zizi==70){
           OrderReturnApply orderReturnApply = orderReturnApplyMapper.queryById(immPurchaseVo.getPurchaseId());
           Double productPrice = orderReturnApply.getProductPrice();
           //是总公司
           if (companyId.equals("0")){
               hqFinancingMapper.updateMoney("0",productPrice,"2");
               UUID uuid = UUID.randomUUID();
               long mostSignificantBits = uuid.getMostSignificantBits();
               HqRicher hqRicher = new HqRicher();
               hqRicher.setRicherId(mostSignificantBits+"");
               hqRicher.setHandFinancingId("0");
               hqRicher.setRicherPrice(productPrice);
               Timestamp timestamp = new Timestamp(new Date().getTime());
               hqRicher.setRicherTime(timestamp);
               hqRicher.setState(2);
               hqRicher.setIsva(1);
               hqRicher.setOper(userName);
               hqRicherMapper.insert(hqRicher);

               ImmPurchase immPurchase = immPurchaseMapper.queryById(immPurchaseVo.getPurchaseId());
               String immId = immPurchase.getImmId();
               System.out.println(immId);
               ImmRicher immRicher = new ImmRicher();
               immRicher.setRicherMoney(productPrice);
               immFinanceMapper.updateOne(immRicher,immId);
           }

       }
        //修改退货订单
        OrderReturnApply orderReturnApply = new OrderReturnApply();
        orderReturnApply.setOrderId(immPurchaseVo.getPurchaseId());
        orderReturnApply.setApplyState(zizi);
        //修改分公司的订单状态
        ImmPurchase immPurchase = new ImmPurchase();
        immPurchase.setPurchaseId(immPurchaseVo.getPurchaseId());
        immPurchase.setIsva(zizi);
        immPurchaseMapper.update(immPurchase);
        //修改总公司的销售状态
        HqMarket hqMarket = new HqMarket();
        hqMarket.setBranch(immPurchaseVo.getPurchaseId());
        hqMarket.setIsva(zizi);
        hqMarketMapper.update(hqMarket);
        int update = orderReturnApplyMapper.update(orderReturnApply);
        return update>0 ? ResponseMessage.success("审核成功") : ResponseMessage.error("审核失败");
    }
}